<?php

/*
 * Author: Quan Van Sinh
 * Email: qvsinh@yahoo.com
 */

session_start();
error_reporting(E_ALL | E_STRICT);
error_reporting(1);
include "init.php";

$action = $_REQUEST['action'];

switch ($action) {
    case 'share_embed':
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_SESSION['client']['id']) {
            include_once MODEL_DIR . 'date.class.php';
            $date_fomat = new dateFormatter();

            $strToExplode = '$$$$$';
            if (trim($_POST['comment']) == 'Viết gì thêm đi nhỉ!') {
                $_POST['comment'] = '';
            }
//$pattern = '/(width|height)="[0-9]*"/i';//both

            preg_match("/width=\"[0-9]*/", $_POST['embed'], $match_width);
            preg_match("/height=\"[0-9]*/", $_POST['embed'], $match_height);
            $width = str_replace('width="', '', $match_width[0]);
            $height = str_replace('height="', '', $match_height[0]);
            if (intval($width) > 400) {
                $new_width = 400;
                $new_height = intval($new_width * $height / $width);

                $_POST['embed'] = preg_replace("/height=\"[0-9]*\"/", 'height="' . $new_height . '"', $_POST['embed']);
                $_POST['embed'] = preg_replace("/width=\"[0-9]*\"/", 'width="' . $new_width . '"', $_POST['embed']);
            }
            if (intval($width) <= 400 && intval($height) > 300) {
                $_POST['embed'] = preg_replace("/height=\"[0-9]*\"/", 'height="300"', $_POST['embed']);
            }

            if (strpos($_POST['embed'], '#####')) {
                $_POST['embed'] = str_replace('#####', '&', $_POST['embed']);
            }
            $content = trim($_POST['embed']) . $strToExplode . trim($_POST['comment']);
            $data['mid'] = $_SESSION['client']['id'];
//$data['geo_id'] = $_SESSION['client']['geo_id'];
            $data['content'] = $content;
            $data['type'] = 2; //bookmark as embed
            $data['create_date'] = date('Y-m-d h:i:s');
            $db->InsertRow('stream', $data);
            $comment_id = $db->GetLastInsertID();

            $arrInfo = array();
            $MEMBER_COMMENT = '';

            $MEMBER_COMMENT = trim($_POST['comment']) ? trim($_POST['comment']) . '<br/>' : '<br/>';

            $MEMBER_COMMENT = $MEMBER_COMMENT . trim($_POST['embed']);
            $arrInfo['COMMENT_ID'] = $comment_id;
            $arrInfo['MEMBER_COMMENT'] = stripslashes($MEMBER_COMMENT);
            $arrInfo['MEMBER_AVATAR'] = $common->get_avatar_thumb($_SESSION['client']['avatar']);
            $arrInfo['MEMBER_FULLNAME'] = $_SESSION['client']['fullname'];
            $arrInfo['SEND_DATE'] = $date_fomat->diff(strtotime($data['create_date']));

            $html = $common->commentHtmlNoSub($arrInfo);

            echo json_encode(array('status' => 'ok', 'id' => $comment_id, 'html' => $html));
        } else {
            echo json_encode(array('status' => 'not_login'));
        }
        break;
    case 'share_link1':
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_SESSION['client']['id']) {
            include_once MODEL_DIR . 'validate.class.php';
            include_once MODEL_DIR . 'outsite.class.php';
            include_once MODEL_DIR . 'date.class.php';
            $date_fomat = new dateFormatter();
            $validate = new validate();
            $outsite = new outsite();

            $strToExplode = '$$$$$';
            if (trim($_POST['comment']) == 'Viết gì thêm đi nhỉ!') {
                $_POST['comment'] = '';
            }

            if (strpos($_POST['link'], '#####')) {
                $_POST['link'] = str_replace('#####', '&', $_POST['link']);
            }

            $content = trim($_POST['imgsrc']) . $strToExplode . trim($_POST['title']) . $strToExplode . trim($_POST['link']) . $strToExplode . trim($_POST['description']) . $strToExplode . trim($_POST['comment']);
            $data['mid'] = $_SESSION['client']['id'];
//$data['geo_id'] = $_SESSION['client']['geo_id'];
            $data['content'] = $content;
            $data['type'] = 1; //bookmark as link
            $data['create_date'] = date('Y-m-d h:i:s');
            $db->InsertRow('stream', $data);
            $comment_id = $db->GetLastInsertID();

            $arrInfo = array();
            $MEMBER_COMMENT = '';

            $MEMBER_COMMENT = trim($_POST['comment']) ? trim($_POST['comment']) . '<br/>' : '<br/>';
            if (trim($_POST['imgsrc']) != "")
                $MEMBER_COMMENT = $MEMBER_COMMENT . '<a target="_blank" href="' . trim($_POST['link']) . '" title="' . trim($_POST['title']) . '" ><img src="' . trim($_POST['imgsrc']) . '" width="130" height="100" > </a>';
            $MEMBER_COMMENT = $MEMBER_COMMENT . '<span class="linktitle"><a target="_blank" href="' . trim($_POST['link']) . '" title="' . trim($_POST['title']) . '" >' . trim($_POST['title']) . '</a></span><span >' . trim($_POST['description']) . '</span>';
            $arrInfo['COMMENT_ID'] = $comment_id;
            $arrInfo['MEMBER_COMMENT'] = stripslashes($MEMBER_COMMENT);
            $arrInfo['MEMBER_AVATAR'] = $common->get_avatar_thumb($_SESSION['client']['avatar']);
            $arrInfo['MEMBER_FULLNAME'] = $_SESSION['client']['fullname'];
            $arrInfo['SEND_DATE'] = $date_fomat->diff(strtotime($data['create_date']));

            $html = $common->commentHtmlNoSub($arrInfo);

            echo json_encode(array('status' => 'ok', 'id' => $comment_id, 'html' => $html));
        } else {
            echo json_encode(array('status' => 'not_login'));
        }
        break;
    case 'attach_link1':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            include_once MODEL_DIR . 'validate.class.php';
            include_once MODEL_DIR . 'outsite.class.php';
            $validate = new validate();
            $outsite = new outsite();
            $result = array();
            $url = trim($_POST['link']);
            if ($validate->isValidUrl($url)) {
                $siteInfo = $outsite->extractDomainData($url);
                $result['status'] = 'ok';
                $result['data'] = $siteInfo;
            } else {
                $result['status'] = 'invalid_url';
            }

            echo json_encode($result);
        }
        break;
    case 'load_more':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            include_once MODEL_DIR . 'member.class.php';
            include_once MODEL_DIR . 'stream.class.php';
            include_once MODEL_DIR . 'date.class.php';
            $member = new member();
            $stream = new stream();
            $date_fomat = new dateFormatter();

            $rsMore = $stream->listCommentFromLastId(5);
            if ($rsMore['rs']) {
                $html = "";
                while ($row = $db->FetchToArray($rsMore['rs'])) {

                    $member_info = $member->getById(intval($row['mid']));
                    $listChildComment = FALSE;
                    $listChildComment = $stream->listChildComment($row['id']);

                    $htmlSubOrSendComment = '';
                    if ($db->NumRows($listChildComment['rs']) == 0) {
                        $arrInfo = array();
                        $arrInfo['COMMENT_ID'] = $row['id'];
                        $htmlSubOrSendComment = $common->commentHtmlSendComment($arrInfo);
                    } else {
                        while ($rowChild = $db->FetchToArray($listChildComment['rs'])) {
                            $memberChildInfo = $member->getById(intval($rowChild['mid']));
                            $arrInfo = array();
                            $arrInfo['SUB_COMMENT_ID'] = $rowChild['id'];
                            $arrInfo['SUB_MEMBER_ID'] = $memberChildInfo['id'];
                            $arrInfo['SUB_MEMBER_FULLNAME'] = $memberChildInfo['fullname'];
                            $arrInfo['SUB_MEMBER_AVATAR'] = $common->get_avatar_thumb($memberChildInfo['avatar']);
                            $arrInfo['SUB_MEMBER_COMMENT'] = $rowChild['content'];
                            $arrInfo['SEND_DATE'] = $date_fomat->diff(strtotime($rowChild['create_date']));
                            $htmlSubOrSendComment .= $common->commentHtmlSubComment($arrInfo);
                        }
                        $arrInfo = array();
                        $arrInfo['COMMENT_ID'] = $row['id'];
                        $arrInfo['SUB_MEMBER_COMMENT'] = $htmlSubOrSendComment;
                        $htmlSubOrSendComment = $common->commentHtmlListSubComment($arrInfo);
                    }

                    if ($row['type'] == 1) {//lien ket
                        $arrContent = explode('$$$$$', $row['content']);
                        $row['content'] = $arrContent[4] ? $arrContent[4] . '<br/>' : '<br/>';
                        if ($arrContent[0] != "" && $arrContent[0] != "undefined")
                            $row['content'] = $row['content'] . '<a target="_blank" href="' . $arrContent[2] . '" title="' . $arrContent[1] . '" ><img src="' . $arrContent[0] . '" width="130" height="100" > </a>';
                        $row['content'] = $row['content'] . '<span class="linktitle"><a target="_blank" href="' . $arrContent[2] . '" title="' . $arrContent[1] . '" >' . $arrContent[1] . '</a></span><span >' . $arrContent[3] . '</span>';
                    } elseif ($row['type'] == 2) {//ma nhung
                        $arrContentEmbed = explode('$$$$$', $row['content']);
                        $row['content'] = $arrContentEmbed[1] ? $arrContentEmbed[1] . '<br/>' : '<br/>';
                        $row['content'] = $row['content'] . $arrContentEmbed[0];
                    }

                    $arrInfo = array();
                    $arrInfo['COMMENT_ID'] = $row['id'];
                    $arrInfo['MEMBER_ID'] = $member_info['id'];
                    $arrInfo['MEMBER_FULLNAME'] = $member_info['fullname'];
                    $arrInfo['MEMBER_AVATAR'] = $common->get_avatar_thumb($member_info['avatar']);
                    $arrInfo['MEMBER_COMMENT'] = $row['content'];
                    $arrInfo['SEND_DATE'] = $date_fomat->diff(strtotime($row['create_date']));
                    $arrInfo['SUB_COMMENT_OR_SEND_COMENT_HTML'] = $htmlSubOrSendComment;
                    $html .= $common->commentHtmlWithSub($arrInfo);

                    $ajax_last_id = $row['id'];
                }
                if ($html == '') {
                    echo json_encode(array('status' => 'end'));
                } else {
                    echo json_encode(array('status' => 'ok', 'last_id' => $ajax_last_id, 'html' => $html));
                }
            }
        }
        break;
    case 'blog_comment':
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_SESSION['client']['id']) {//phai login thi moi comment duoc
            include_once MODEL_DIR . 'date.class.php';
            $date_fomat = new dateFormatter();

            $comment = trim($_POST['comment']);
            $parent_id = intval($_POST['parent_id']);
            if (!empty($comment) && $comment != 'Bạn đang nghĩ gì?') {
                $data = array();
                if ($parent_id > 0) {
                    $data['parent_id'] = $parent_id;
                }
                $data['mid'] = $_SESSION['client']['id'];
                //$data['geo_id'] = $_SESSION['client']['geo_id'];
                $data['content'] = $comment;
                $data['create_date'] = date('Y-m-d h:i:s');
                $db->InsertRow('stream', $data);
                $comment_id = $db->GetLastInsertID();
                $act_data = array();
                $act_data['mid'] = $_SESSION['client']['id'];
                $act_data['content'] = $common->limitWord($comment, 5);
                if ($parent_id > 0) {

                    $act_data['obj_id'] = $parent_id;
                } else {
                    $act_data['obj_id'] = $comment_id;
                }
                $act_data['create_date'] = date('Y-m-d h:i:s');
                $act_data['type'] = 1;
                $db->InsertRow('activity', $act_data);
                $arrInfo = array();
                if ($parent_id > 0) {
                    $arrInfo['SUB_COMMENT_ID'] = $comment_id;
                    $arrInfo['SUB_MEMBER_ID'] = $_SESSION['client']['id'];
                    $arrInfo['SUB_MEMBER_COMMENT'] = stripslashes($comment);
                    $arrInfo['SUB_MEMBER_AVATAR'] = $common->get_avatar_thumb($_SESSION['client']['avatar']);
                    $arrInfo['SUB_MEMBER_FULLNAME'] = $_SESSION['client']['fullname'];
                    $arrInfo['SUB_SEND_DATE'] = $date_fomat->diff(strtotime($data['create_date']));
                    $html = $common->commentHtmlSubComment($arrInfo);
                } else {
                    $arrInfo['COMMENT_ID'] = $comment_id;
                    $arrInfo['MEMBER_COMMENT'] = stripslashes($comment);
                    $arrInfo['MEMBER_AVATAR'] = $common->get_avatar_thumb($_SESSION['client']['avatar']);
                    $arrInfo['MEMBER_FULLNAME'] = $_SESSION['client']['fullname'];
                    $arrInfo['SEND_DATE'] = $date_fomat->diff(strtotime($data['create_date']));
                    $html = $common->commentHtmlNoSub($arrInfo);
                }

                echo json_encode(array('status' => 'ok', 'id' => $comment_id, 'html' => $html));
            }
        } else {
            echo json_encode(array('status' => 'not_login'));
        }
        break;
    case 'like_this':
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_SESSION['client']['id']) {//phai login thi moi comment duoc
            include_once MODEL_DIR . 'stream.class.php';
            $stream = new stream();
            $mid = $_SESSION['client']['id'];
            $stream_id = intval($_POST['stream_id']);
            if ($stream->likeStream($mid, $stream_id)) {
                echo json_encode(array('status' => 'ok', 'id' => $stream_id));
            }
        } else {
            echo json_encode(array('status' => 'not_login'));
        }
        break;
    case 'save_profile':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $option = intval($_POST['option']);

            $nickname = trim($_POST['nickname']);
            $first_name = trim($_POST['first_name']);
            $last_name = trim($_POST['last_name']);
            $gender = intval($_POST['gender']);
            $dob = trim($_POST['dob']);
            $dob = $common->dateFormatEN($dob, "/");
            $is_married = intval($_POST['is_married']);
            $current_address = trim($_POST['current_address']);
            $hometown = trim($_POST['hometown']);
            $city = intval($_POST['city']);
            $district = intval($_POST['district']);
            $about = trim($_POST['about']);

            $high_school = trim($_POST['high_school']);
            $university = trim($_POST['university']);
            $job = trim($_POST['job']);

            $music = trim($_POST['music']);
            $movie = trim($_POST['movie']);
            $sport = trim($_POST['sport']);
            $book = trim($_POST['book']);
            $other_hobby = trim($_POST['other_hobby']);

            $mid = $_SESSION['client']['id'];
            $data = array();
            $data['last_update'] = date('Y-m-d h:i:s');
            if ($option == 1) {
                if ($district == 0) {
                    echo 'error_district';
                    die;
                }

                $data['nickname'] = $nickname;
                $data['first_name'] = $first_name;
                $data['last_name'] = $last_name;
                $data['gender'] = $gender;
                $data['dob'] = $dob;
                $data['is_married'] = $is_married;
                $data['current_address'] = $current_address;
                $data['hometown'] = $hometown;
                $data['geo_id'] = $district;
                $data['about'] = $about;
            }
            if ($option == 2) {
                $data['high_school'] = $high_school;
                $data['university'] = $university;
                $data['job'] = $job;
            }
            if ($option == 3) {
                $data['music'] = $music;
                $data['movie'] = $movie;
                $data['sport'] = $sport;
                $data['book'] = $book;
                $data['other_hobby'] = $other_hobby;
            }

            /*
              if (strlen($mapLatLon)) {
              $data['map_latlon'] = $mapLatLon;
              }
              if (strlen($mapAddress)) {
              $data['map_address'] = $mapAddress;
              }
             */
            if ($option == 1 || $option == 2 || $option == 3) {
                $db->UpdateRows('member', $data, " id=$mid ");
            }
            echo "ok";
        }
        break;
    case 'create_album':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $name = trim($_POST['name']);
            $share_for = $_POST['share_for'];
            if (empty($name)) {
                echo json_encode(array('status' => 'name_empty'));
                die;
            }

            $mid = $_SESSION['client']['id'];
            $data = array();
            $data['name'] = $name;
            $data['share_for'] = $share_for;
            $data['mid'] = $mid;
            $data['create_date'] = date('Y-m-d h:i:s');
            $data['last_update'] = $data['create_date'];
            $db->InsertRow('album', $data);
            $aid = $db->GetLastInsertID();

            echo json_encode(array('status' => 'ok', 'album_id' => $aid));
        }
        break;
    case 'update_album':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $id = intval($_POST['id']);
            $name = trim($_POST['name']);
            $permision = intval($_POST['permision']);

            if (empty($name)) {
                echo json_encode(array('status' => 'name_empty'));
                die;
            }
            if ($id <= 0) {
                echo json_encode(array('status' => 'no_album'));
                die;
            }

            $mid = $_SESSION['client']['id'];
            $data = array();
            $data['name'] = $name;
            $data['permision'] = $permision;
            $data['mid'] = $mid;
            $data['last_update'] = date('Y-m-d h:i:s');
            $db->UpdateRows('album', $data, " id=$id AND mid=$mid ");
            echo json_encode(array('status' => 'ok'));
        }
        break;
    case 'share_album':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $aid = trim($_POST['aid']);
            $share_for = $_POST['share_for'];
            $mid = $_SESSION['client']['id'];
            $data = array();
            $data['share_for'] = $share_for;
            $data['last_update'] = date('Y-m-d h:i:s');
            $db->UpdateRows('album', $data, " id=$aid AND mid=$mid ");

            echo json_encode(array('status' => 'ok'));
        }
        break;
    case 'album_notify':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $aid = trim($_POST['aid']);
            $share_for = $_POST['share_for'];
            $mid = $_SESSION['client']['id'];
            $aname = trim($_POST['aname']);
            $data_notify = array();
            $data_notify['content'] = $mid . '$$$$$' . $aid . '$$$$$' . $aname;
            $data_notify['type'] = 3; //notify image upload
            $data_notify['create_date'] = date('Y-m-d h:i:s');
            $data_notify['share_for'] = $share_for;
            $data_notify['mid'] = $mid;
            $db->InsertRow('stream', $data_notify);
        }
        break;
    case 'member_edit_image':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            defined('DOCUMENT_ROOT') || define('DOCUMENT_ROOT', $_SERVER ['DOCUMENT_ROOT']);
            $res = json_decode(stripslashes($_POST['jsondata']), true);
            /* get data */
            $count_images = count($res['images']);
            $background = DOCUMENT_ROOT . '/' . $res['images'][0]['src'];
            $upload_dir = dirname($background);
            $filename = time() . '-' . basename($background);
            $backgroundW = $res['images'][0]['width'];
            $backgroundH = $res['images'][0]['height'];
            $ext = end(explode(".", $res['images'][0]['src']));
            list($width_ogrin, $height_ogrin, $type, $attr) = getimagesize($background);
            /* Create new object */
            $img_bg = new Imagick();
            $img_bg->newImage($width_ogrin, $height_ogrin, new ImagickPixel('white'), $ext);
            for ($i = 1; $i < $count_images; ++$i) {
                $ext_item = end(explode(".", $res['images'][$i]['src']));
                $itemRotation = $res['images'][$i]['rotation'];
                $itemW = $res['images'][$i]['width'] * $width_ogrin / $backgroundW;
                $itemH = $res['images'][$i]['height'] * $width_ogrin / $backgroundW;
                $itemY = $res['images'][$i]['top'] * $width_ogrin / $backgroundW;
                $itemX = $res['images'][$i]['left'] * $width_ogrin / $backgroundW;
                $item = new Imagick();
                $item->readImage(DOCUMENT_ROOT . '/' . $res['images'][$i]['src']);
                $item->resizeImage($itemW, $itemH, Imagick::FILTER_LANCZOS, 1);

                if (!$res['images'][$i]['roundcorner']) {
                    $item->roundCorners(5, 5);
                }
                if ($res['images'][$i]['shadow']) {
                    /* Clone the current object */
                    $shadow = $item->clone();
                    /* Set image background color to black
                      (this is the color of the shadow) */
                    $shadow->setImageBackgroundColor(new ImagickPixel('black'));
                    /* Create the shadow */
                    $shadow->shadowImage(80, 3, 5, 5);
                    /* Imagick::shadowImage only creates the shadow.
                      That is why the original image is composited over it */
                    $shadow->compositeImage($item, Imagick::COMPOSITE_OVER, 0, 0);
                    $item->destroy();
                    if (!is_null($itemRotation)) {
                        $shadow->rotateImage(new ImagickPixel('white'), $itemRotation);
                        $itemY-= ( $shadow->getImageHeight() - $itemH) / 2;
                        $itemX-= ( $shadow->getImageWidth() - $itemW) / 2;
                    }
                    $img_bg->compositeImage($shadow, Imagick::COMPOSITE_SRCOVER, $itemX, $itemY);
                    $shadow->destroy();
                } else {
                    if (!is_null($itemRotation)) {
                        $item->rotateImage(new ImagickPixel('white'), $itemRotation);
                        $itemY-= ( $item->getImageHeight() - $itemH) / 2;
                        $itemX-= ( $item->getImageWidth() - $itemW) / 2;
                    }
                    $img_bg->compositeImage($item, Imagick::COMPOSITE_SRCOVER, $itemX, $itemY);
                    $item->destroy();
                }
            }

            $bg_layer = new Imagick(DOCUMENT_ROOT . '/' . $res['images'][0]['src']);
            $img_bg->compositeImage($bg_layer, Imagick::COMPOSITE_SRCOVER, 0, 0);
            $bg_layer->destroy();
            if (is_array($res['texts']) && !empty($res['texts'])) {
                foreach ($res['texts'] as $key => $value) {
                    $t_size = ($value['size']) ? round($value['size'] * $width_ogrin / $backgroundW) : 25;
                    $t_color = ($value['color']) ? $value['color'] : 'white';
                    $t_font = ($value['font']) ? DOCUMENT_ROOT . "/font/unicode/" . $value['font'] . ".ttf" : 'arial';
                    $t_top = ($value['top']) ? ($value['top'] - 8) * $width_ogrin / $backgroundW : 0;
                    $t_left = ($value['left']) ? ($value['left'] - 8) * $width_ogrin / $backgroundW : 0;
                    $t_rotation = ($value['rotation']) ? $value['rotation'] : 0;
                    $t_text = ($value['text']) ? $value['text'] : '';
                    if ($t_text != '') {
                        $draw = new ImagickDraw();
                        /*                         * * set the font ** */
                        $draw->setFont($t_font);
                        /*                         * * set the font size ** */
                        $draw->setFontSize($t_size);
                        $draw->setFillColor($t_color);
                        /*                         * * add some transparency ** */
                        //$draw->setFillAlpha(1.0);
                        /*                         * * set gravity to the center ** */
                        $draw->setGravity(Imagick::GRAVITY_NORTHWEST);
                        if ($value['shadow']) {
                            $textcanvas = new Imagick();
                            $textprops = $textcanvas->queryFontMetrics($draw, $t_text, true); //you need to turn multiline on, the last param
                            $text_w = $textprops['textWidth'];
                            $text_h = $textprops['textHeight'];
                            $textcanvas->newImage($text_w + 10, $text_h + 10, new ImagickPixel('transparent')); //give 20 pixel spaces around the text
                            $textcanvas->setImageFormat('png');
                            $textcanvas->annotateImage($draw, 10, 10, 0, $t_text);
                            // create shadow
                            $t_shadow = $textcanvas->clone();
                            $t_shadow->setImageBackgroundColor(new ImagickPixel('black'));
                            $t_shadow->shadowImage(100, 1, 2, 2);
                            $t_shadow->compositeImage($textcanvas, Imagick::COMPOSITE_OVER, 0, 0);
                            if ($t_rotation) {
                                $t_shadow->rotateImage(new ImagickPixel('transparent'), $t_rotation);
                                $t_top-= ( $t_shadow->getImageHeight() - ($text_h + 10)) / 2;
                                $t_left-= ( $t_shadow->getImageWidth() - ($text_w + 10)) / 2;
                            }
                            $img_bg->compositeImage($t_shadow, Imagick::COMPOSITE_SRCOVER, $t_left, $t_top);
                        } else {
                            $img_bg->annotateImage($draw, $t_left, $t_top, $t_rotation, $t_text);
                        }
                        $draw->destroy();
                    }
                }
            }
            $img_bg->writeImage($upload_dir . '/' . $filename);
            $img_bg->destroy();
            switch ($ext) {
                case "pdf": $ctype = "application/pdf";
                    break;
                case "exe": $ctype = "application/octet-stream";
                    break;
                case "zip": $ctype = "application/zip";
                    break;
                case "doc": $ctype = "application/msword";
                    break;
                case "xls": $ctype = "application/vnd.ms-excel";
                    break;
                case "ppt": $ctype = "application/vnd.ms-powerpoint";
                    break;
                case "gif": $ctype = "image/gif";
                    break;
                case "png": $ctype = "image/png";
                    break;
                case "jpeg":
                case "jpg": $ctype = "image/jpg";
                    break;
                default: $ctype = "application/force-download";
            }
            $fsize = filesize($upload_dir . '/' . $filename); 
            header("Pragma: public"); // required
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private", false); // required for certain browsers
            header("Content-Type: $ctype");
            header("Content-Disposition: attachment; filename=postcard.png;");
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: " . $fsize);
            ob_clean();
            flush();
            readfile($upload_dir . '/' . $filename);
        } else {
            include_once MODEL_DIR . 'image.class.php';
            $clsimage = new image();
            $im_bg = imagecreatetruecolor($width_ogrin, $height_ogrin);
            $bc = imagecolorallocate($im_bg, 255, 255, 255);
            imagefill($im_bg, 0, 0, $bc);
            /* the other images */
            for ($i = 1; $i < $count_images; ++$i) {
                $item = DOCUMENT_ROOT . '/' . $res['images'][$i]['src'];
                $ext_item = end(explode(".", $res['images'][$i]['src']));
                $function_item = $clsimage->returnCorrectFunction($ext_item);
                if (!is_null($function_item)) {
                    $image = $function_item($item);
                    $width = imagesx($image);
                    $height = imagesy($image);
                    $itemRotation = $res['images'][$i]['rotation'];
                    $itemW = $res['images'][$i]['width'] * $width_ogrin / $backgroundW;
                    $itemH = $res['images'][$i]['height'] * $width_ogrin / $backgroundW;
                    $itemY = $res['images'][$i]['top'] * $width_ogrin / $backgroundW;
                    $itemX = $res['images'][$i]['left'] * $width_ogrin / $backgroundW;
                    $image_p = imagecreatetruecolor($itemW, $itemH);
                    $backColor = imagecolorallocate($image_p, 255, 255, 255);
                    imagefill($image_p, 0, 0, $backColor);
                    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $itemW, $itemH, $width, $height);
                    imagedestroy($image);
                    if (!is_null($itemRotation)) {
                        $image_p = imagerotate($image_p, -$itemRotation, $backColor);
                        $itemY-= ( imagesy($image_p) - $itemH) / 2;
                        $itemX-= ( imagesx($image_p) - $itemW) / 2;
                    }
                    imagecopy($im_bg, $image_p, $itemX, $itemY, 0, 0, imagesx($image_p), imagesy($image_p));
                    imagedestroy($image_p);
                }
            }
            $function = $clsimage->returnCorrectFunction($ext);
            $image_l = $function($background);
            $width = imagesx($image_l);
            $height = imagesy($image_l);
            $image_last = imagecreatetruecolor($width_ogrin, $height_ogrin);
            $clsimage->setTransparency($image_l, $image_last, $ext);
            imagecopyresampled($image_last, $image_l, 0, 0, 0, 0, $width_ogrin, $height_ogrin, $width, $height);
            imagedestroy($image_l);
            imagecopy($im_bg, $image_last, 0, 0, 0, 0, $width_ogrin, $height_ogrin);
            imagedestroy($image_last);
            $clsimage->parseImage($ext, $im_bg, $upload_dir . '/' . $filename);
            imagedestroy($im_bg);
        }


        echo json_encode(array('status' => 'ok', 'src' => $upload_dir . '/' . $filename));
        break;
    case 'create_avatar':
        error_reporting(0);
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $targ_w = 58;
            $targ_h = 58;
            $jpeg_quality = 95;
            $mid = $_SESSION['client']['id'];
            $sql = "SELECT avatar FROM member WHERE id=$mid";
            $rsAvatar = $db->QueryFirst($sql);
            $avatar_file = trim($rsAvatar['avatar']);
            $source_file = str_replace('a-', '', $avatar_file);
            $src = $_SERVER ['DOCUMENT_ROOT'] . $source_file;

            $img_r = imagecreatefromjpeg($src);
            $dst_r = ImageCreateTrueColor($targ_w, $targ_h);

            imagecopyresampled($dst_r, $img_r, 0, 0, $_POST['x'], $_POST['y'], $targ_w, $targ_h, $_POST['w'], $_POST['h']);
            if (file_exists($_SERVER ['DOCUMENT_ROOT'] . $avatar_file)) {
                unlink($_SERVER ['DOCUMENT_ROOT'] . $avatar_file);
            }
            header('Content-type: image/jpeg');
            imagejpeg($dst_r, $_SERVER ['DOCUMENT_ROOT'] . $avatar_file, $jpeg_quality);
            imagedestroy($dst_r);
            echo $avatar_file;
            exit;
        }
        break;
    case 'get_district':
        include_once MODEL_DIR . 'geographic.class.php';
        $geo = new geographic();
        $id = intval($_POST['city_id']);
        $rs = $geo->getByParentId($id);
        $html = '<option value="0">Chọn quận/huyện của bạn...</option>';
        while ($row = $db->FetchToArray($rs)) {
            $html .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
        }
        echo $html;
        break;
    case 'get_member_avatar':
        include_once MODEL_DIR . 'member.class.php';
        $member = new member();
        echo $member->getAvatar();
        break;
    case 'send_mail':
        include_once MODEL_DIR . 'member.class.php';
        $member = new member();
        echo $member->sendMail();
        break;
    case 'delete_image':
        include_once MODEL_DIR . 'member.class.php';
        $member = new member();
        $image_id = intval($_POST['image_id']);
        echo $member->deleteImage($image_id);
        break;
    case 'delete_album':
        include_once MODEL_DIR . 'member.class.php';
        $member = new member();
        $album_id = intval($_POST['album_id']);
        echo $member->deleteAlbum($album_id);
        break;
    case 'change_password':
        include_once MODEL_DIR . 'member.class.php';
        $member = new member();
        echo $member->changePassword();
        break;
    case 'image_comment':
        $comment = trim($_POST['comment']);
        $image_id = intval($_POST['image_id']);
        if (intval($_SESSION['client']['id'])) {
            if ($image_id && !empty($comment)) {
                include_once MODEL_DIR . 'date.class.php';
                $date_fomat = new dateFormatter();
                $create_date = date('Y-m-d h:i:s');
                $mid = $_SESSION['client']['id'];
                $data = array();
                $data['comment'] = $comment;
                $data['image_id'] = $image_id;
                $data['mid'] = $mid;
                $data['fullname'] = $_SESSION['client']['fullname'];
                $data['create_date'] = $create_date;
                $db->InsertRow('image_comment', $data);
                $arrInfo = array();
                $arrInfo['SUB_COMMENT_ID'] = "";
                $arrInfo['SUB_MEMBER_ID'] = $mid;
                $arrInfo['SUB_MEMBER_COMMENT'] = $comment;
                $arrInfo['SUB_MEMBER_AVATAR'] = $_SESSION['client']['avatar'];
                $arrInfo['SUB_MEMBER_FULLNAME'] = $_SESSION['client']['fullname'];
                $arrInfo['SUB_SEND_DATE'] = $date_fomat->diff(strtotime($data['create_date']));
                $html = $common->commentHtmlSubComment($arrInfo);
                echo json_encode(array('status' => 'ok', 'html' => $html));
            }
        } else {
            echo json_encode(array('status' => 'not_login'));
        }
        break;
    case 'view_image':
        $option = trim($_POST['option']);
        $image_id = intval($_POST['image_id']);
        $mid = intval($_POST['mem_id']);
        error_reporting(E_ALL);
        if ($image_id) {
            include_once MODEL_DIR . 'date.class.php';
            $date_fomat = new dateFormatter();
            if ($option == 'next') {
                $sql = "SELECT * FROM images WHERE mid=$mid AND id>$image_id ORDER BY id LIMIT 1";
            } else {//pre
                $sql = "SELECT * FROM images WHERE mid=$mid AND id<$image_id ORDER BY id DESC LIMIT 1";
            }
            $arrRs = $db->QueryFirst($sql);
            if (intval($arrRs['id']) < 1 && $option == 'next') {// quay ve anh dau tien
                $sql = "SELECT * FROM images WHERE mid=$mid ORDER BY id LIMIT 1";
                $arrRs = $db->QueryFirst($sql);
            }
            if (intval($arrRs['id']) < 1 && $option == 'prev') {// quay ve anh dau tien
                $sql = "SELECT * FROM images WHERE mid=$mid ORDER BY id DESC LIMIT 1";
                $arrRs = $db->QueryFirst($sql);
            }
            $listComment = $db->query("SELECT * FROM image_comment WHERE 1 AND image_id=" . $arrRs['id']);
            $html = '';
            if ($listComment) {
                while ($rowChild = $db->FetchToArray($listComment)) {
                    $memberChildInfo = $member->getById(intval($rowChild['mid']));
                    $arrInfo = array();
                    $arrInfo['COMMENT_ID'] = $rowChild['id'];
                    $arrInfo['MEMBER_ID'] = $memberChildInfo['id'];
                    $arrInfo['MEMBER_FULLNAME'] = $memberChildInfo['fullname'];
                    $arrInfo['MEMBER_AVATAR'] = $memberChildInfo['avatar'];
                    $arrInfo['MEMBER_COMMENT'] = $rowChild['comment'];
                    $arrInfo['SEND_DATE'] = $date_fomat->diff(strtotime($rowChild['create_date']));
                    $html .= $common->commentImage($arrInfo);
                }
            }
            if ($arrRs['id']) {
                echo json_encode(array('status' => 'ok', 'arrRs' => $arrRs, 'html' => $html));
            } else {
                echo json_encode(array('status' => 'end_image'));
            }
        }

        break;
    case 'update_image_view':
        $image_id = intval($_POST['image_id']);

        if ($image_id) {

            $member->updateImageView($image_id);
        }

        break;
    case 'register':
        include MODEL_DIR . 'validate.class.php';
        $validate = new validate();
        $fullname = trim($_POST['fullname']);
        $email = $_POST['email'];
        $password = md5($_POST['password']);
        $re_password = md5($_POST['re_password']);

        if (empty($fullname)) {
            echo 'fullname_empty';
            die;
        }
        if (!$validate->email($email)) {
            echo 'email_error';
            die;
        } else {
            $sqlCheck = "SELECT * FROM member WHERE email='$email'";
            $rsCheck = $db->QueryFirst($sqlCheck);
            if ($rsCheck['id']) {
                echo 'email_exist';
                die;
            }
        }

        if (strlen($_POST['password']) < 6) {
            echo 'password_short';
            die;
        }
        if (empty($_POST['password'])) {
            echo 'password_empty';
            die;
        }
        if ($password != $re_password) {
            echo 'password_not_match';
            die;
        }

        $create_date = date('Y-m-d h:i:s');
        $data = array();
        $data['fullname'] = $fullname;
        $data['email'] = $email;
        $data['password'] = $password;
        $data['create_date'] = $create_date;
        $db->InsertRow('member', $data);
        echo 'ok';
        break;
    case 'create_group':
        $name = trim($_POST['group_name']);
        $desc = trim($_POST['desc']);

        if ($member->addGroup($name, $desc))
            echo 'ok';
        else
            echo 'fail';
        break;
    case 'addFriend':
        $friend_id = trim($_POST['friend_id']);
        $group_id = trim($_POST['group_id']);
        print_r($member->addFriend($friend_id, $group_id));
        break;
    case 'removeFriendFromGroup':
        $friend_id = trim($_POST['friend_id']);
        $group_id = trim($_POST['group_id']);
        print_r($member->removeFriendFromGroup($friend_id, $group_id));
        break;
    case 'removeFriend':
        $friend_id = trim($_POST['friend_id']);
        $groups = trim($_POST['groups']);
        $mid = $_SESSION['client']['id'];
        if ($member->removeFriend($mid, $friend_id, $groups))
            echo 'ok';
        else
            echo 'fail';
        break;
    case 'crop_img':

        include_once MODEL_DIR . 'image.class.php';

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $clsimage = new image();
            $mid = $_SESSION['client']['id'];
            $sql = "SELECT avatar FROM member WHERE id=$mid";
            $rsAvatar = $db->QueryFirst($sql);
            $file = trim($rsAvatar['avatar']);
            $source_file = str_replace('a-', '', $file);
            $src = $_SERVER ['DOCUMENT_ROOT'] . $source_file;


            list($width, $height) = getimagesize($_POST["imageSource"]);
            $viewPortW = $_POST["viewPortW"];
            $viewPortH = $_POST["viewPortH"];
            $pWidth = $_POST["imageW"];
            $pHeight = $_POST["imageH"];
            $ext = end(explode(".", $_POST["imageSource"]));
            $function = $clsimage->returnCorrectFunction($ext);
            $image = $function($_SERVER ['DOCUMENT_ROOT'] . $_POST["imageSource"]);
            $width = imagesx($image);
            $height = imagesy($image);
// Resample

            $image_p = imagecreatetruecolor($pWidth, $pHeight);
            $clsimage->setTransparency($image, $image_p, $ext);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $pWidth, $pHeight, $width, $height);
            imagedestroy($image);
            $widthR = imagesx($image_p);
            $hegihtR = imagesy($image_p);

            if ($_POST["imageRotate"]) {
                $angle = 360 - $_POST["imageRotate"];
                $image_p = imagerotate($image_p, $angle, 0);
                $pWidth = imagesx($image_p);
                $pHeight = imagesy($image_p);
            }
            if ($pWidth > $_POST["viewPortW"]) {
                if ($_POST["imageX"] > 0) {
                    $src_x = 0;
                    $dst_x = $_POST["imageX"];
                } else {
                    $src_x = abs(abs($_POST["imageX"]) - abs(($_POST["imageW"] - $pWidth) / 2));
                    $dst_x = 0;
                }
            } else {
                $src_x = 0;
                $dst_x = $_POST["imageX"] + (($_POST["imageW"] - $pWidth) / 2);
            }
            if ($pHeight > $_POST["viewPortH"]) {
                if ($_POST["imageY"]) {
                    $src_y = 0;
                    $dst_y = $_POST["imageY"];
                } else {
                    $src_y = abs($_POST["imageY"] - abs(($_POST["imageH"] - $pHeight) / 2));
                    $dst_y = 0;
                }
            } else {
                $src_y = 0;
                $dst_y = $_POST["imageY"] + (($_POST["imageH"] - $pHeight) / 2);
            }

//echo $src_x."-".$src_y."|".$dst_x."-".$dst_y;


            $viewport = imagecreatetruecolor($_POST["viewPortW"], $_POST["viewPortH"]);
            $clsimage->setTransparency($image_p, $viewport, $ext);
//parseImage($ext, $image_p, "uploads/avatar/before_viewport" . time() . "." . $ext);
            imagecopy($viewport, $image_p, $dst_x, $dst_y, $src_x, $src_y, $pWidth, $pHeight);
//parseImage($ext, $viewport, "uploads/avatar/before_selector" . time() . "." . $ext);
            imagedestroy($image_p);

            $selectorX = $_POST["selectorX"];
            $selectorY = $_POST["selectorY"];

            $selector = imagecreatetruecolor($_POST["selectorW"], $_POST["selectorH"]);
            $clsimage->setTransparency($viewport, $selector, $ext);
            imagecopy($selector, $viewport, 0, 0, $selectorX, $selectorY, $_POST["viewPortW"], $_POST["viewPortH"]);

            if (file_exists($_SERVER ['DOCUMENT_ROOT'] . $file)) {
                unlink($_SERVER ['DOCUMENT_ROOT'] . $file);
            }


            $clsimage->parseImage($ext, $selector, $_SERVER ['DOCUMENT_ROOT'] . $file);
            imagedestroy($viewport);
//Return value
            echo $file;

            exit;
        }
        break;
    case 'crop_image':

        include_once MODEL_DIR . 'image.class.php';

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $clsimage = new image();
            $img_id = $_POST["id"];
            $sql = "SELECT image FROM images WHERE id=$mid";
            $rs = $db->QueryFirst($sql);
            $file = trim($rs['image']);
            $src = $_SERVER ['DOCUMENT_ROOT'] . $source_file;


            list($width, $height) = getimagesize($src);
            $viewPortW = $_POST["viewPortW"];
            $viewPortH = $_POST["viewPortH"];
            $pWidth = $_POST["imageW"];
            $pHeight = $_POST["imageH"];
            $ext = end(explode(".", $file));
            $function = $clsimage->returnCorrectFunction($ext);
            $image = $function($src);
            $width = imagesx($image);
            $height = imagesy($image);
// Resample

            $image_p = imagecreatetruecolor($pWidth, $pHeight);
            $clsimage->setTransparency($image, $image_p, $ext);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $pWidth, $pHeight, $width, $height);
            imagedestroy($image);
            $widthR = imagesx($image_p);
            $hegihtR = imagesy($image_p);

            if ($_POST["imageRotate"]) {
                $angle = 360 - $_POST["imageRotate"];
                $image_p = imagerotate($image_p, $angle, 0);
                $pWidth = imagesx($image_p);
                $pHeight = imagesy($image_p);
            }
            if ($pWidth > $_POST["viewPortW"]) {
                if ($_POST["imageX"] > 0) {
                    $src_x = 0;
                    $dst_x = $_POST["imageX"];
                } else {
                    $src_x = abs(abs($_POST["imageX"]) - abs(($_POST["imageW"] - $pWidth) / 2));
                    $dst_x = 0;
                }
            } else {
                $src_x = 0;
                $dst_x = $_POST["imageX"] + (($_POST["imageW"] - $pWidth) / 2);
            }
            if ($pHeight > $_POST["viewPortH"]) {
                if ($_POST["imageY"]) {
                    $src_y = 0;
                    $dst_y = $_POST["imageY"];
                } else {
                    $src_y = abs($_POST["imageY"] - abs(($_POST["imageH"] - $pHeight) / 2));
                    $dst_y = 0;
                }
            } else {
                $src_y = 0;
                $dst_y = $_POST["imageY"] + (($_POST["imageH"] - $pHeight) / 2);
            }

//echo $src_x."-".$src_y."|".$dst_x."-".$dst_y;


            $viewport = imagecreatetruecolor($_POST["viewPortW"], $_POST["viewPortH"]);
            $clsimage->setTransparency($image_p, $viewport, $ext);
//parseImage($ext, $image_p, "uploads/avatar/before_viewport" . time() . "." . $ext);
            imagecopy($viewport, $image_p, $dst_x, $dst_y, $src_x, $src_y, $pWidth, $pHeight);
//parseImage($ext, $viewport, "uploads/avatar/before_selector" . time() . "." . $ext);
            imagedestroy($image_p);

            $selectorX = $_POST["selectorX"];
            $selectorY = $_POST["selectorY"];

            $selector = imagecreatetruecolor($_POST["selectorW"], $_POST["selectorH"]);
            $clsimage->setTransparency($viewport, $selector, $ext);
            imagecopy($selector, $viewport, 0, 0, $selectorX, $selectorY, $_POST["viewPortW"], $_POST["viewPortH"]);

            if (file_exists($_SERVER ['DOCUMENT_ROOT'] . $file)) {
                unlink($_SERVER ['DOCUMENT_ROOT'] . $file);
            }


            $clsimage->parseImage($ext, $selector, $_SERVER ['DOCUMENT_ROOT'] . $file);
            imagedestroy($viewport);
//Return value
            echo $file;

            exit;
        }
        break;
    case 'search_friend':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $keyword = isset($_POST['keyword']) ? trim($_POST['keyword']) : "";
            $group_id = trim($_POST['group_id']);
            $rsFriend = $member->searchFriend($keyword, $group_id);
            $rsGroup = $member->getAllGroup();
            $arrGroup = array();
            while ($row = $db->FetchToArray($rsGroup)) {
                $arrGroup[$row['id']] = $row;
            }
            $num_records = $db->NumRows($rsFriend);
            $html = "";
            while ($row = $db->FetchToArray($rsFriend)) {
                $arrBelongGroup = explode(",", $row['groups']);
                $arrInfo = array();
                $arrInfo['MEMBER_ID'] = $row['id'];
                $arrInfo['MEMBER_FULLNAME'] = $row['fullname'];
                $arrInfo['MEMBER_AVATAR'] = $common->get_avatar_thumb($row['avatar']);
                $arrInfo['MEMBER_EMAIL'] = $row['email'];
                $arrInfo['MEMBER_URL'] = DOMAIN . "/thanh-vien?mid=" . $row['id'];
                $arrInfo['MEMBER_GROUPS'] = $row['groups'];
                $box_action = '<div class="sys_action">
                                    <div class="sys_act_icon"></div>  
                                </div>
                                <div class="box_action">
                                    <div class="overlap"></div>
                                    <ul rel="' . $row['id'] . '" lang="' . $row['groups'] . '">';
                foreach ($arrGroup as $value) {
                    $checked = (in_array($value['id'], $arrBelongGroup)) ? "checked" : "";
                    $box_action.='<li>
                    <input type="checkbox" ' . $checked . ' id="' . $value['id'] . '"/>' . $value['group_name'] . '
                    </li>  ';
                }
                $box_action.='<li class="sep"></li>                                       
                    <li class="rm"><div>Thôi không là bạn</div></li>
                    </ul>
                    </div>';
                $arrInfo['SYS_ACTION'] = $box_action;
                $html .= $common->friendListHtml($arrInfo);
            }
            if ($html == '') {
                echo json_encode(array('status' => 'end'));
            } else {
                echo json_encode(array('status' => 'ok', 'html' => $html, 'num' => $num_records));
            }
        }
        break;
    case 'load_header_mail':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $type = isset($_POST['type']) ? trim($_POST['type']) : "message";
            $InboxUnRead = $member->mailInbox($type, -1, 5);
            $mail_ids = "";
            $html = '';
            foreach ($InboxUnRead as $key => $row) {
                if ($row['id'] > 0) {
                    if ($mail_ids != "")
                        $mail_ids.=",";
                    $mail_ids.=$row['id'];
                    $html.='
<li >
    <a class="thumb" href="' . DOMAIN . "/thanh-vien?mid=" . $row['sender']['id'] . '">
       <img src="' . $common->get_avatar_thumb($row['sender']['avatar']) . '">
    </a>
    <div class="left">
        <h4><a href="' . DOMAIN . "/thanh-vien?mid=" . $row['sender']['id'] . '">' . $row['sender']['fullname'] . '</a></h4>
        ' . $row['content'] . '
    </div>
</li>';
                }
            }

            if ($html == '') {
                echo json_encode(array('status' => 'end'));
            } else {
                $html = '<ul class="left" >' . $html . '</ul>';
                echo json_encode(array('status' => 'ok', 'html' => $html));
                if ($mail_ids != "")
                    $member->maskIsRead($mail_ids);
            }
        }
        break;
    case 'upload_card_temp':
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $attachs = $_FILES['Filedata'];
            $upload_dir = $_SERVER ['DOCUMENT_ROOT'] . CARD_TEMP_UPLOAD_PATH;
            if (!file_exists($upload_dir)) {
                mkdir($upload_dir, 0777, true);
            }

            $now = time();
            $arrPhotoNameExplode = explode('.', $attachs["name"]);
            $photoExtention = $arrPhotoNameExplode[(count($arrPhotoNameExplode) - 1)];
            $filename = date("Ymd") . "_" . uniqid() . '.' . strtolower($photoExtention);
            if (move_uploaded_file($attachs["tmp_name"], $upload_dir . $filename)) {
                list($width, $height, $type, $attr) = getimagesize($upload_dir . $filename);
                echo json_encode(array('filename' => CARD_TEMP_UPLOAD_PATH . $filename, "width" => $width, "height" => $height));
            }else
                echo "false";
        }
        break;
}
?>